package com.wyc.serviceMap.remote;

import com.wyc.internalCommon.constant.AmapConfigConstant;
import com.wyc.internalCommon.request.DirectionDTO;
import com.wyc.internalCommon.request.ForecastPriceDTO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 调用高德api获取距离和时长
 */
@Service
public class MapDirectionClient {

    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 调用高德地图第三方接口
     * 获取距离和时长
     * @param forecastPriceDTO
     * @return
     */
    public DirectionDTO direction(ForecastPriceDTO forecastPriceDTO){

        // 出发地纬度
        String depLatitude = forecastPriceDTO.getDepLatitude();
        // 出发地经度
        String depLongitude = forecastPriceDTO.getDepLongitude();
        // 目的地纬度
        String destLatitude = forecastPriceDTO.getDestLatitude();
        // 目的地经度
        String destLongitude = forecastPriceDTO.getDestLongitude();

        // 拼接路径
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(AmapConfigConstant.DIRECTION_URL_PREFIX);
        urlBuilder.append("?");
        urlBuilder.append("origin="+depLongitude+","+depLatitude);
        urlBuilder.append("&");
        urlBuilder.append("destination="+destLongitude+","+destLatitude);
        urlBuilder.append("&");
        urlBuilder.append("extensions=base");
        urlBuilder.append("&");
        urlBuilder.append("output=json");
        urlBuilder.append("&");
        urlBuilder.append("key="+amapKey);

        // 调用接口
        ResponseEntity<String> directionEntity = restTemplate.getForEntity(urlBuilder.toString(), String.class);
        String directionString = directionEntity.getBody();

        // 解析地图返回值
        DirectionDTO directionDto = parseDirectionEntity(directionString);

        return directionDto;
    }

    /**
     * 解析第三方地图api返回值
     * @param directionString
     * @return
     */
    private DirectionDTO parseDirectionEntity(String directionString) {

        DirectionDTO directionDTO = new DirectionDTO();

        try {

            JSONObject jsonObject = JSONObject.fromObject(directionString);
            if (jsonObject.has(AmapConfigConstant.STATUS)){
                int status = jsonObject.getInt(AmapConfigConstant.STATUS);
                if (status == 1){
                    if (jsonObject.has(AmapConfigConstant.ROUTE)){
                        JSONObject routeObject = jsonObject.getJSONObject(AmapConfigConstant.ROUTE);
                        if (routeObject.has(AmapConfigConstant.PATHS)){
                            JSONArray pathsArray = routeObject.getJSONArray(AmapConfigConstant.PATHS);

                            JSONObject pathObject = pathsArray.getJSONObject(0);
                            // 距离
                            if (pathObject.has(AmapConfigConstant.DISTANCE)){
                                int distance = pathObject.getInt(AmapConfigConstant.DISTANCE);
                                directionDTO.setDistance(distance);
                            }
                            // 时长
                            if (pathObject.has(AmapConfigConstant.DURATION)){
                                int duration = pathObject.getInt(AmapConfigConstant.DURATION);
                                directionDTO.setDuration(duration);
                            }

                        }
                    }
                }
            }

        }catch (Exception e){

        }

        return directionDTO;
    }

}
